import { AhoCorasick } from './ahocorasick.js';

function searchTestOne() {
    const text = 'should find keyword1 at position 19 and keyword2 at position 30.';
    const ahoCorasick = new AhoCorasick(['hero', 'heroic', 'heroism']);
    let searchResult;
    const startTime = Date.now();
    for (let index = 0; index < 10000; index++) {
        searchResult = ahoCorasick.search(text);
    }
    const endTime = Date.now();
    if (JSON.stringify(searchResult) != '[]') {
        throw new Error("AhoCorasick_Search_Func_0100 error");
    }
    print(`AhoCorasick_Search_Func_0100: ${endTime - startTime} ms`);
}

function searchTestTwo() {
    const text = 'www.yahoo.com';
    const ahoCorasick = new AhoCorasick(['.com.au', '.com']);
    let searchResult;
    const startTime = Date.now();
    for (let index = 0; index < 10000; index++) {
        searchResult = ahoCorasick.search(text);
    }
    const endTime = Date.now();
    if (JSON.stringify(searchResult) != '[[12,[".com"]]]') {
        throw new Error("AhoCorasick_Search_Func_0200 error");
    }
    print(`AhoCorasick_Search_Func_0200: ${endTime - startTime} ms`);
}

function searchTestThree() {
    const text = 'Bla 😁 bla 😀 1 😀 - 😀😁😀-';
    const ahoCorasick = new AhoCorasick(['bla', '😁', '😀', '°□°', 'w', '┻━┻']);
    let searchResult;
    const startTime = Date.now();
    for (let index = 0; index < 10000; index++) {
        searchResult = ahoCorasick.search(text);
    }
    const endTime = Date.now();
    if (JSON.stringify(searchResult) != '[[5,["😁"]],[9,["bla"]],[12,["😀"]],[17,["😀"]],[22,["😀"]],[24,["😁"]],[26,["😀"]]]') {
        throw new Error("AhoCorasick_Search_Func_0300 error");
    }
    print(`AhoCorasick_Search_Func_0300: ${endTime - startTime} ms`);
}

searchTestOne();
searchTestTwo();
searchTestThree();
